package com.wxjfh.servlet;

import com.wxjfh.Utils.DataUtils;
import com.wxjfh.bean.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/addstudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //请求参数内容设置编码格式
        req.setCharacterEncoding("utf-8");
        //获取用户输入数据
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String string_age = req.getParameter("age");
        if(!string_age.equals("")){
            Integer age = Integer.parseInt(string_age);
            //校验用户输入数据
            if(name == null  || sex == null || age == null ){
                resp.sendRedirect("AddStudent.jsp?status=1");
                return;
            }
            //存储到学生类中、将实例学生类存到集合
            Student student = new Student(name,sex,age);
            DataUtils.addStu(student);
            System.out.println(student);
            //继续在该页面添加
            resp.sendRedirect("AddStudent.jsp？status=2");
        }else{
            resp.sendRedirect("AddStudent.jsp?status=1");
        }
    }
}
